package xyz.lwm.lazycat.netpipe.util;

import java.security.SecureRandom;

/**
 * 随机生成用户名密码
 *
 * @author lwm
 */
public abstract class SecureCredentialGenerator {

    private static final String USERNAME_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

    private static final String PASSWORD_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%&*()_+-=[]|;:,.<>?";

    public static String generateUsername(int length) {
        return generateRandomString(USERNAME_CHARS, length);
    }

    public static String generatePassword(int length) {
        return generateRandomString(PASSWORD_CHARS, length);
    }

    private static String generateRandomString(String charSet, int length) {
        SecureRandom random = new SecureRandom();
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            int randomIndex = random.nextInt(charSet.length());
            sb.append(charSet.charAt(randomIndex));
        }
        return sb.toString();
    }

}